package com.yztc.ui.cook;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.yztc.bean.cook.CateGoryListBean;
import com.yztc.bean.cook.CookerListBean;
import com.yztc.bean.cook.ListFocusBean;
import com.yztc.bean.cook.NewFoodTypeBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.LoginActivity;
import com.yztc.ui.WebViewActivity;
import com.yztc.ui.cook.categoryinfo.CategoryInfoActivity;
import com.yztc.ui.cook.jiekou.OnItemClickListener;
import com.yztc.utils.ImageHelper;

import java.util.List;

import static com.yztc.ui.cook.categoryinfo.CategoryInfoActivity.getListAnim;
import static com.yztc.ui.cook.categoryinfo.CategoryInfoModle.a;

/**
 * Created by My on 2016/10/6.
 */
public class CookFragment extends Fragment implements CookConstract.View{
    private View view;
    private CookPresenter presenter;
    private ImageView iv_top,iv_login;
    private ListView lv_newfood;
    private NewFoodAdapter foodAdapter;
    private CategoryAdapter categoryAdapter;
    private CookerAdapter cookerAdapter;
    private RecyclerView recyclerView1,recyclerView2;
    private LinearLayoutManager mLayoutManager1;
    private LinearLayoutManager mLayoutManager2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_cook,null);
        iv_top= (ImageView) view.findViewById(R.id.cook_img);
        lv_newfood= (ListView) view.findViewById(R.id.listVIew_cook);
        recyclerView1= (RecyclerView) view.findViewById(R.id.recyclerView_food);
        recyclerView2= (RecyclerView) view.findViewById(R.id.recyclerView_cooker);
        iv_login= (ImageView) view.findViewById(R.id.iv_msg_enter);

        presenter = new CookPresenter(this);
        presenter.getData();
        presenter.getNewFoodData();
        //跳转到消息中心
        toMsgenter();
        return view;
    }

    private void toMsgenter() {
        iv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onGetDataSuccess(final List<CateGoryListBean> listCategory, final List<ListFocusBean> listFocus, final List<CookerListBean> chefListBeen) {
        ImageHelper.getNormalImage(getActivity(),listFocus.get(0).getPicUrl(),iv_top);
        //创建线性布局
        mLayoutManager1 = new LinearLayoutManager(getActivity());
        //垂直方向
        mLayoutManager1.setOrientation(OrientationHelper.HORIZONTAL);
        //给RecyclerView设置布局管理器
        recyclerView1.setLayoutManager(mLayoutManager1);
        //创建线性布局
        mLayoutManager2 = new LinearLayoutManager(getActivity());
        //垂直方向
        mLayoutManager2.setOrientation(OrientationHelper.HORIZONTAL);
        //给RecyclerView设置布局管理器
        recyclerView2.setLayoutManager(mLayoutManager2);
        Log.i("wx","-------listCategoty-------"+listCategory.size());
        Log.i("wx","-------listFocusBeanList-------"+listFocus.size());
        Log.i("wx","-------listCooker-------"+chefListBeen.size());
        categoryAdapter=new CategoryAdapter(getActivity(),listCategory);
        cookerAdapter=new CookerAdapter(getActivity(),chefListBeen);
        recyclerView1.setAdapter(categoryAdapter);
        recyclerView2.setAdapter(cookerAdapter);
        //顶部大图的跳转事件
        iv_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(getActivity(),WebViewActivity.class);
                Bundle bundle = new Bundle();
                Log.i("wx","------URL------>"+listFocus.get(0).getLink());
                bundle.putString("link",listFocus.get(0).getLink());
                Log.i("wx","------title------>"+listFocus.get(0).getTitle());
                bundle.putString("title",listFocus.get(0).getTitle());
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });
        //食谱分类的点击事件
        categoryAdapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(),CategoryInfoActivity.class);
                intent.putExtra("id", listCategory.get(position).getCategoryId());
                a=listCategory.get(position).getCategoryId();
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        //入驻大厨的点击事件
        cookerAdapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent2=new Intent(getActivity(),WebViewActivity.class);
                Bundle bundle = new Bundle();
                Log.i("wx","------URL------>"+chefListBeen.get(position).getUrl());
                bundle.putString("link", chefListBeen.get(position).getUrl());
                bundle.putString("title","春播-"+chefListBeen.get(position).getName());
                intent2.putExtras(bundle);
                startActivity(intent2);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onGetNewFoodDataSuccess(final List<NewFoodTypeBean> NewFoodTypeBeanList) {
        foodAdapter=new NewFoodAdapter(getActivity(),NewFoodTypeBeanList);
        lv_newfood.setAdapter(foodAdapter);

        lv_newfood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent3=new Intent(getActivity(),WebViewActivity.class);
                Bundle bundle = new Bundle();
                Log.i("wx","------URL------>"+NewFoodTypeBeanList.get(position).getUrl());
                bundle.putString("link", NewFoodTypeBeanList.get(position).getUrl());
                bundle.putString("title","菜谱详情");
                intent3.putExtras(bundle);
                startActivity(intent3);
            }
        });
    }

    @Override
    public void onGetDataFail(Throwable error) {

    }

    @Override
    public void onGetNewFoodDataFail(Throwable error) {

    }

}
